// (C) Copyright to Kevin. Applied to MIT LICENSE

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent/Public/ProceduralMeshComponent.h"
#include "BlockEngine/DataStructures.h"

#include "ChunkActor.generated.h"

UCLASS()
class BLOCKWORLD_API AChunkActor : public AActor
{
	GENERATED_BODY()

private:
	bool initialized = false;

	UPROPERTY(EditAnywhere, Category = "BlockEngine")
	bool changed = false;

	UPROPERTY(VisibleAnywhere, Category = "BlockEngine")
		FChunkCoordinate chunk_coordinate;

	ChunkData* chunk_data = NULL;

	int32 chunk_index = -1;

	friend class ABlockEngineGameMode;

public:	
	// Sets default values for this actor's properties
	AChunkActor();
	~AChunkActor();

	AChunkActor * neighbors[6] = { NULL, NULL, NULL, NULL, NULL, NULL };

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable)
		FChunkCoordinate GetChunkCoordinate();

	ChunkData* GetChunkData();

	void ScheduleUpdate();

	void Initialize(FChunkCoordinate chunk_coordinate, ChunkData* chunk_data);

	// Mark for re-rendering
	void MarkChanged();

	const bool& IsInitialized() const;

	void SetChunkBlock(int x, int y, int z, uint16 id, uint16 meta);

};
